import Vue from "vue";
import VueRouter from "vue-router";
import Home from "../views/home/home.vue";
// @  是一个绝对路径 指向的是src
import Login from "@/views/login/login.vue";
import Users from "@/views/users/users.vue";
import Rights from "@/views/rights/rights.vue";
import Welcome from "@/views/welcome/welcome.vue";
import Roles from "@/views/roles/roles.vue";
import  Reports from "@/views/reports/reports.vue"
import  Orders from "@/views/orders/orders.vue"



Vue.use(VueRouter);

const routes = [
  { path: "/", redirect: "/home" },
  {
    path: "/login",
    name: "login",
    component: Login,
  },
  {
    path: "/home",
    name: "home",
    component: Home,
    redirect:{
      path:'/welcome'
    },
    children:[
      {
        path: "/welcome",
        name: "welcome",
        component: Welcome,
      },
      {
        path:"/rights",
        name:'rights',
        component:Rights
      },
      {
        path: "/roles",
        name: "roles",
        component: Roles,
      },
      {
        path: "/users",
        name: "users",
        component: Users,
      },
      {
        path: "/reports",
        name: "reports",
        component: Reports,
      },
      {
        path: "/orders",
        name: "orders",
        component: Orders,
      },


      
    ]
  },
  
];

const router = new VueRouter({
  routes,
});

router.beforeEach((to, from, next) => {
  let token = window.localStorage.getItem("myToken");
  // 表示已经登录了
  if (token) {
    next();
  } else {
    //没有token  表示没有登录
    if (to.path == "/login") {
      next();
    } else {
      next({ path: "/login" });
    }
  }
});

export default router;
